package com.cskaoyan.market.controller.admin;

import com.cskaoyan.market.annotation.RequiresPermissionsDesc;
import com.cskaoyan.market.bo.GoodsCombineBo;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.service.AdminGoodsService;
import com.cskaoyan.market.util.ResponseUtil;
import com.cskaoyan.market.vo.BrandVo;
import com.cskaoyan.market.vo.CategoryVo;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/goods")
public class AdminGoodsController extends HttpServlet {

    @Autowired
    AdminGoodsService adminGoodsService;


    @GetMapping("catAndBrand")
    @RequiresPermissions("admin:goods:catAndBrand")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"},button = "catAndBrand")
    public Object catAndBrand() {

        List<CategoryVo> marketCategoryList = adminGoodsService.categoryList();
        List<BrandVo> marketBrandList = adminGoodsService.brandList();
        HashMap<String, List> data = new HashMap<>();
        data.put("brandList", marketBrandList);
        data.put("categoryList", marketCategoryList);
       return ResponseUtil.ok(data);
    }

    @GetMapping("detail")
    @RequiresPermissions("admin:goods:detail")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"},button = "详情")
    public Object detail(String id) {
        MarketGoods marketGoods = adminGoodsService.getGoodsDetail(id);
        List<MarketGoodsAttribute> marketGoodsAttributes = adminGoodsService.getGoodsAttributes(id);
        List<MarketGoodsSpecification> marketGoodsSpecifications = adminGoodsService.getGoodsSpecifications(id);
        List<MarketGoodsProduct> marketGoodsProducts = adminGoodsService.getGoodsProducts(id);

        List<Integer> marketCategoryIds = new ArrayList<>();
        MarketCategory marketCategory = adminGoodsService.findPid(marketGoods.getCategoryId());
        marketCategoryIds.add(marketCategory.getPid());
        marketCategoryIds.add(marketGoods.getCategoryId());

        Map<String, Object> data = new HashMap<>();
        data.put("goods", marketGoods);
        data.put("categoryIds", marketCategoryIds);
        data.put("attributes", marketGoodsAttributes);
        data.put("specifications", marketGoodsSpecifications);
        data.put("products", marketGoodsProducts);
        return ResponseUtil.ok(data);
    }

    @GetMapping("list")
    @RequiresPermissions("admin:goods:list")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"},button = "查询")
    public Object list(Integer page,Integer limit,String sort ,String order,Integer goodsId,String goodsSn,String name) {

        List<MarketGoods> marketGoodsList = adminGoodsService.list(page, limit, sort, order, goodsId, goodsSn, name);
        return ResponseUtil.okList(marketGoodsList);
    }


    @PostMapping("create")
    @RequiresPermissions("admin:goods:create")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"},button = "新增")
    public Object create(@RequestBody GoodsCombineBo combineBo) {
        /*String requestBody = req.getReader().readLine();
        ObjectMapper mapper = JacksonUtil.getObjectMapper();
        GoodsCombineBo combineBo = mapper.readValue(requestBody, GoodsCombineBo.class);*/
        MarketGoods goods = combineBo.getGoods();
        List<MarketGoodsAttribute> attributes=combineBo.getAttributes();
        List<MarketGoodsSpecification> specifications=combineBo.getSpecifications();
        List<MarketGoodsProduct> products=combineBo.getProducts();
        adminGoodsService.insert(goods,attributes,specifications,products);
        return ResponseUtil.ok();
    }

    @PostMapping("update")
    @RequiresPermissions("admin:goods:update")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"},button = "更新")
    public Object update(@RequestBody GoodsCombineBo combineBo) {
        /*String requestBody = HttpUtil.getRequestBody(req);
        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        GoodsCombineBo combineBo = objectMapper.readValue(requestBody, GoodsCombineBo.class);*/

        MarketGoods goods = combineBo.getGoods();
        List<MarketGoodsAttribute> attributes = combineBo.getAttributes();
        List<MarketGoodsSpecification> specifications = combineBo.getSpecifications();
        List<MarketGoodsProduct> products = combineBo.getProducts();

        adminGoodsService.updateGoods(goods, attributes, products, specifications);
        return ResponseUtil.ok();
    }

    @PostMapping("delete")
    @RequiresPermissions("admin:goods:delete")
    @RequiresPermissionsDesc(menu = {"商品管理", "商品列表"},button = "删除")
    public Object delete(@RequestBody Map map) {
        Integer id = (Integer) map.get("id");
        adminGoodsService.delete(id);
        return ResponseUtil.ok();
    }
}
